package com.bitc.crash.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bitc.crash.common.ErrorCode;
import com.bitc.crash.exception.BusinessException;
import com.bitc.crash.mapper.CommodityMapper;
import com.bitc.crash.model.entity.Commodity;
import com.bitc.crash.model.vo.CommodityVO;
import com.bitc.crash.service.CommodityService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 商品服务实现类
 */
@Service
@Slf4j
public class CommodityServiceImpl extends ServiceImpl<CommodityMapper, Commodity> implements CommodityService {

    @Override
    public void validCommodity(Commodity commodity, boolean add) {
        if (commodity == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        
        String name = commodity.getName();
        String description = commodity.getDescription();
        
        // 创建时，参数不能为空
        if (add) {
            if (StringUtils.isAnyBlank(name, description)) {
                throw new BusinessException(ErrorCode.PARAMS_ERROR);
            }
        }
        
        // 有参数则校验
        if (StringUtils.isNotBlank(name) && name.length() > 50) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "商品名称过长");
        }
        if (StringUtils.isNotBlank(description) && description.length() > 500) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "商品描述过长");
        }
    }

    @Override
    public QueryWrapper<Commodity> getQueryWrapper(Commodity commodityQuery) {
        QueryWrapper<Commodity> queryWrapper = new QueryWrapper<>();
        if (commodityQuery == null) {
            return queryWrapper;
        }
        
        Long id = commodityQuery.getId();
        String name = commodityQuery.getName();
        String description = commodityQuery.getDescription();
        Long categoryId = commodityQuery.getCategoryId();
        Integer status = commodityQuery.getStatus();
        String tags = commodityQuery.getTags();
        
        // 拼接查询条件
        queryWrapper.eq(id != null, "id", id);
        queryWrapper.like(StringUtils.isNotBlank(name), "name", name);
        queryWrapper.like(StringUtils.isNotBlank(description), "description", description);
        queryWrapper.eq(categoryId != null, "category_id", categoryId);
        queryWrapper.eq(status != null, "status", status);
        queryWrapper.like(StringUtils.isNotBlank(tags), "tags", tags);
        
        queryWrapper.orderByDesc("sort_weight");
        queryWrapper.orderByDesc("create_time");
        
        return queryWrapper;
    }

    @Override
    public CommodityVO getCommodityVO(Commodity commodity) {
        if (commodity == null) {
            return null;
        }
        CommodityVO commodityVO = new CommodityVO();
        BeanUtils.copyProperties(commodity, commodityVO);
        return commodityVO;
    }

    @Override
    public Page<CommodityVO> getCommodityVOPage(Page<Commodity> commodityPage) {
        List<Commodity> commodityList = commodityPage.getRecords();
        Page<CommodityVO> commodityVOPage = new Page<>(commodityPage.getCurrent(), commodityPage.getSize(), commodityPage.getTotal());
        if (commodityList.isEmpty()) {
            return commodityVOPage;
        }
        
        // 填充信息
        List<CommodityVO> commodityVOList = commodityList.stream()
                .map(this::getCommodityVO)
                .collect(Collectors.toList());
        
        commodityVOPage.setRecords(commodityVOList);
        return commodityVOPage;
    }
}